package mobi.greenplate.theweb.web;

import java.io.Serializable;
import java.util.List;
import javax.annotation.ManagedBean;
import javax.inject.Inject;
import org.springframework.context.annotation.Scope;
import org.springframework.web.context.WebApplicationContext;

import org.deltaset.faces.FacesUtils;
import org.deltaset.faces.PageHelper;

import mobi.greenplate.theweb.domain.RequestRide;
import mobi.greenplate.theweb.repository.RequestRideRepository;

//for : enum types (and many-to-one)

import mobi.greenplate.theweb.domain.RideState;    
import mobi.greenplate.theweb.user.UserUtils;


@ManagedBean("requestRideBean")
@Scope(WebApplicationContext.SCOPE_REQUEST)
public class RequestRideBean implements Serializable {

    private static final String OUTCOME_DETAIL = "/secured/requestRide/detail?faces-redirect=true&requestRideId=%s";
    private static final String OUTCOME_LIST = "/secured/requestRide/list?faces-redirect=true";
    private static final long serialVersionUID = 1L;


    @Inject
    public RequestRideBean(final RequestRideRepository modelRepository) {
        this.modelRepository = modelRepository;
    }
    private final RequestRideRepository modelRepository;
	
	
	public RequestRide loadModel(String id) {
        if (null != id && !"".equals(id.trim())) {
            return modelRepository.findById(Long.valueOf(id));
        }
        return new RequestRide();
    }

    public boolean isNew(RequestRide model) {
        return null == model.getId();
    }

    public String delete(RequestRide model) {
        modelRepository.delete(model.getId());
        return OUTCOME_LIST;
    }
    
   public void deleteAndReset(RequestRide model, PageHelper<RequestRide> pageHelper) {
        delete(model);
        pageHelper.reset();
    }

    public String save(RequestRide model) {
        final RequestRide saved = modelRepository.save(model,UserUtils.currentUser());
        return String.format(OUTCOME_DETAIL, saved.getId().toString());
    }

    //always here. actually provides support for one-to-Many
    public void setModelTo(RequestRide model, String valueExpression){
        FacesUtils.setValueExpression(valueExpression,model);
    }
	

    public RideState[] getRideStateValues() {
        return RideState.values();
    }


/// List handlers

    public PageHelper<RequestRide> all(String pageNum) {
        return all.load(pageNum);
    }
    private PageHelper<RequestRide> all = PageHelper.newInstance(new AllFinder());

    private static class AllFinder implements PageHelper.Finder<RequestRide> {

        private static final long serialVersionUID = 1L;

        public int counter(Object... params) {
            return FacesUtils.getManagedBean("requestRideRepository", RequestRideRepository.class).countAll();
        }

        public List<RequestRide> finder(int firstResult, int maxResult, Object... params) {
            return FacesUtils.getManagedBean("requestRideRepository", RequestRideRepository.class).
                    findAll(firstResult, maxResult);
        }
    }
	

	//ManyToOne - by requester
    public PageHelper<RequestRide> byRequester(String pageNum, Long modelId) {
        return byRequester.load(pageNum, modelId);
    }
    private PageHelper<RequestRide> byRequester = PageHelper.newInstance(new ByRequesterFinder());

    private static class ByRequesterFinder implements PageHelper.Finder<RequestRide> {

        private static final long serialVersionUID = 1L;

        public int counter(Object... params) {
            return FacesUtils.getManagedBean("requestRideRepository", RequestRideRepository.class).countByRequester((Long) params[0]);
        }

        public List<RequestRide> finder(int firstResult, int maxResult, Object... params) {
            return FacesUtils.getManagedBean("requestRideRepository", RequestRideRepository.class).
                    findByRequester(firstResult, maxResult, (Long) params[0]);
        }
    }

	//ManyToOne - by startAddress
    public PageHelper<RequestRide> byStartAddress(String pageNum, Long modelId) {
        return byStartAddress.load(pageNum, modelId);
    }
    private PageHelper<RequestRide> byStartAddress = PageHelper.newInstance(new ByStartAddressFinder());

    private static class ByStartAddressFinder implements PageHelper.Finder<RequestRide> {

        private static final long serialVersionUID = 1L;

        public int counter(Object... params) {
            return FacesUtils.getManagedBean("requestRideRepository", RequestRideRepository.class).countByStartAddress((Long) params[0]);
        }

        public List<RequestRide> finder(int firstResult, int maxResult, Object... params) {
            return FacesUtils.getManagedBean("requestRideRepository", RequestRideRepository.class).
                    findByStartAddress(firstResult, maxResult, (Long) params[0]);
        }
    }

	//ManyToOne - by endAddress
    public PageHelper<RequestRide> byEndAddress(String pageNum, Long modelId) {
        return byEndAddress.load(pageNum, modelId);
    }
    private PageHelper<RequestRide> byEndAddress = PageHelper.newInstance(new ByEndAddressFinder());

    private static class ByEndAddressFinder implements PageHelper.Finder<RequestRide> {

        private static final long serialVersionUID = 1L;

        public int counter(Object... params) {
            return FacesUtils.getManagedBean("requestRideRepository", RequestRideRepository.class).countByEndAddress((Long) params[0]);
        }

        public List<RequestRide> finder(int firstResult, int maxResult, Object... params) {
            return FacesUtils.getManagedBean("requestRideRepository", RequestRideRepository.class).
                    findByEndAddress(firstResult, maxResult, (Long) params[0]);
        }
    }

	

    
}
